package com.wytec.xywzxw.utils.redis;

import jakarta.annotation.PostConstruct;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

@Component
public class RedisUtils {
    @Resource
    JedisPool jedisPool;

    Jedis jedis;
    private static RedisUtils redisUtils;

    @PostConstruct
    public void init() {
        redisUtils = this;
        redisUtils.jedisPool = jedisPool;
    }
    public Jedis getJedis(){
        if (jedis == null){
            jedis = redisUtils.jedisPool.getResource();
        }
        return jedis;
    }

    public void set(String key,String value,int expired){
        getJedis().set(key, value);
        if(expired != 0){
            getJedis().expire(key,expired);
        }
    }
    public void set(String key,String value){
        int expired = 0;
        set(key, value,expired);
    }
    public String get(String key){
        return getJedis().get(key);
    }
    public Long del(String key){
        return getJedis().del(key);
    }

}
